<?php

	include_once("../../globals.php");
	include_once("$srcdir/api.inc");
	include_once("$srcdir/forms.inc");
	//global $num_forms;
	$values = $_REQUEST;
	
	echo "<pre>";
		print_r($values);
	echo "</pre>";
	
	echo "<pre>";
		print_r($_SESSION);
	echo "</pre>";
	
	ECHO "sESSION ".$_SESSION['num_forms']."/////";
	
	//if ( !$encounter) { // comes from globals.php
	//	die("Internal error: we do not seem to be in an encounter!");
	//}
	
	$pid = $_SESSION['pid'];
	
	function check_form($encounter, $form_name){
		$query = "SELECT * FROM forms WHERE encounter = $encounter AND formdir = \"$form_name\"";
		$res = sqlStatement($query);
		$row = sqlFetchArray($res);
		return $row['form_id'];
	}
	
	function delete_past($formid){
		$query = "DELETE FROM form_pupillometry WHERE id = $formid OR form_related = $formid";
		sqlStatement($query);
	}
	
	function save_new($forms_id){
		global $values, $formid, $pid;
			/*echo "<pre>";
				print_r($forms_id);
			echo "</pre>";*/
		$first = true;
		foreach( $forms_id as $form_number ){
			/*if($values[$form_number."_date_1"]==''){*/ $date = "NOW()";/* }else{ $date = $values[$form_number."_date_1"]; }*/
			$scheduled_value = $values[$form_number."_scheduled_1"];
			if($values[$form_number."_pupillometry_1"]==''){ $pupil_size_minimum = 0; }else{ $pupil_size_minimum = $values[$form_number."_pupillometry_1"]; }
			if($values[$form_number."_pupillometry_2"]==''){ $pupil_size_maximum = 0; }else{ $pupil_size_maximum = $values[$form_number."_pupillometry_2"]; }			
			$pupil_reaction = $values[$form_number."_pupillometry_3"];
			$pupil_shape = $values[$form_number."_pupillometry_4"];
			$pupil_location = $values[$form_number."_pupillometry_5"];
			$eyelids = $values[$form_number."_extocular_1"];
			$eyeshades = $values[$form_number."_extocular_2"];;
			$conjuctiva = $values[$form_number."_extocular_3"];;
			$sclera = $values[$form_number."_extocular_4"];;
			$cornea_edema = $values[$form_number."_slitlamp_1"];
			$cornea_staining_erosion = $values[$form_number."_slitlamp_2"];
			$anterior_chamber_cell = $values[$form_number."_slitlamp_3"];
			$anterior_chamber_flare = $values[$form_number."_slitlamp_4"];
			$iris_atrophy_erosion = $values[$form_number."_slitlamp_5"];
			$iris_peaking = $values[$form_number."_slitlamp_6"];
			$iris_rubeosis = $values[$form_number."_slitlamp_7"];
			$lens_opacity = $values[$form_number."_slitlamp_8"];
			$slit_lamp_other = $values[$form_number."_slitlamp_9"];
			if($values[$form_number."_corrected_1"]==''){ $snellen = 0; }else{ $snellen = $values[$form_number."_corrected_1"]; }
			if($values[$form_number."_corrected_2"]==''){ $finger_count = 0; }else{ $finger_count = $values[$form_number."_corrected_2"]; }
			if($values[$form_number."_corrected_3"]==''){ $hand_movement = 0; }else{ $hand_movement = $values[$form_number."_corrected_3"]; }
			$superior_temporal = $values[$form_number."_gonioscopy_1"];
			$superior_nasal = $values[$form_number."_gonioscopy_2"];
			$inferior_temporal = $values[$form_number."_gonioscopy_3"];
			$inferior_nasal = $values[$form_number."_gonioscopy_4"];
			if($values[$form_number."_antchamber_1"]==''){ $anterior_chamber_depth = 0; }else{ $anterior_chamber_depth = $values[$form_number."_antchamber_1"]; }
			if($values[$form_number."_antchamber_2"]==''){ $intraocular_pressure = 0; }else{ $intraocular_pressure = $values[$form_number."_antchamber_2"]; }
			if($values[$form_number."_antchamber_3"]==''){ $specular_microscopy = 0; }else{ $specular_microscopy = $values[$form_number."_antchamber_3"]; }
			$dilated_fundus_examination = $values[$form_number."_dilated_1"];
			$infrared_digital_photo = $values[$form_number."_dilated_2"];
			
			//Insert into pupillometry info
			if($first && $formid){
				$query = "INSERT INTO form_pupillometry(id, pid, date, scheduled, pupil_size_minimum, pupil_size_maximum, pupil_reaction, pupil_shape, pupil_location, eyelids, eyeshades, conjuctiva, sclera, cornea_edema, cornea_staining_erosion, anterior_chamber_cell, anterior_chamber_flare, iris_atrophy_erosion, iris_peaking, iris_rubeosis, lens_opacity, slit_lamp_other, snellen, finger_count, hand_movement, superior_temporal, superior_nasal, inferior_temporal, inferior_nasal, anterior_chamber_depth, intraocular_pressure, specular_microscopy,  dilated_fundus_examination, infrared_digital_photo) ".
				"VALUES ($formid, $pid $date, $scheduled_value,  $pupil_size_minimum, $pupil_size_maximum, $pupil_reaction, $pupil_shape, $pupil_location, $eyelids, $eyeshades, $conjuctiva, $sclera, $cornea_edema, $cornea_staining_erosion, $anterior_chamber_cell, $anterior_chamber_flare, $iris_atrophy_erosion, $iris_peaking, $iris_rubeosis, $lens_opacity, $slit_lamp_other, $snellen, $finger_count, $hand_movement, $superior_temporal, $superior_nasal, $inferior_temporal, $inferior_nasal, $anterior_chamber_depth, $intraocular_pressure, $specular_microscopy,  $dilated_fundus_examination, $infrared_digital_photo)";		
				$first = false;
			}elseif (!$first && $formid){
				$query = "INSERT INTO form_pupillometry(form_related, pid,  date, scheduled, pupil_size_minimum, pupil_size_maximum, pupil_reaction, pupil_shape, pupil_location, eyelids, eyeshades, conjuctiva, sclera, cornea_edema, cornea_staining_erosion, anterior_chamber_cell, anterior_chamber_flare, iris_atrophy_erosion, iris_peaking, iris_rubeosis, lens_opacity, slit_lamp_other, snellen, finger_count, hand_movement, superior_temporal, superior_nasal, inferior_temporal, inferior_nasal, anterior_chamber_depth, intraocular_pressure, specular_microscopy,  dilated_fundus_examination, infrared_digital_photo) ".
				"VALUES ($formid, $pid, $date, $scheduled_value,  $pupil_size_minimum, $pupil_size_maximum, $pupil_reaction, $pupil_shape, $pupil_location, $eyelids, $eyeshades, $conjuctiva, $sclera, $cornea_edema, $cornea_staining_erosion, $anterior_chamber_cell, $anterior_chamber_flare, $iris_atrophy_erosion, $iris_peaking, $iris_rubeosis, $lens_opacity, $slit_lamp_other, $snellen, $finger_count, $hand_movement, $superior_temporal, $superior_nasal, $inferior_temporal, $inferior_nasal, $anterior_chamber_depth, $intraocular_pressure, $specular_microscopy,  $dilated_fundus_examination, $infrared_digital_photo)";		
			}elseif (!$formid){
				$query = "INSERT INTO form_pupillometry( pid, date, scheduled, pupil_size_minimum, pupil_size_maximum, pupil_reaction, pupil_shape, pupil_location, eyelids, eyeshades, conjuctiva, sclera, cornea_edema, cornea_staining_erosion, anterior_chamber_cell, anterior_chamber_flare, iris_atrophy_erosion, iris_peaking, iris_rubeosis, lens_opacity, slit_lamp_other, snellen, finger_count, hand_movement, superior_temporal, superior_nasal, inferior_temporal, inferior_nasal, anterior_chamber_depth, intraocular_pressure, specular_microscopy,  dilated_fundus_examination, infrared_digital_photo) ".
				"VALUES ( $pid, $date, $scheduled_value,  $pupil_size_minimum, $pupil_size_maximum, $pupil_reaction, $pupil_shape, $pupil_location, $eyelids, $eyeshades, $conjuctiva, $sclera, $cornea_edema, $cornea_staining_erosion, $anterior_chamber_cell, $anterior_chamber_flare, $iris_atrophy_erosion, $iris_peaking, $iris_rubeosis, $lens_opacity, $slit_lamp_other, $snellen, $finger_count, $hand_movement, $superior_temporal, $superior_nasal, $inferior_temporal, $inferior_nasal, $anterior_chamber_depth, $intraocular_pressure, $specular_microscopy,  $dilated_fundus_examination, $infrared_digital_photo)";			
			}
			echo $query."\n";
			sqlStatement($query);
		}
	}
	
	
	$formid = $_REQUEST['id'];
	
	//if( $values['save'] ){
		if( $formid ){
			echo "si formid";
			//$query = "DELETE FROM form_pupillometry WHERE forms_id = '$formid'";
			//echo $query;
			//sqlStatement($query);
			delete_past($formid);
			$continue = true;
			$temp = 1;
				if($values['save_type']=='save_new'){
					$form_numbers[] = $temp;
				}
				
				while($continue){
					if(intval($temp) < intval($_SESSION['num_forms'])){
						$temp += 1;
						$form_numbers[] = $temp;
						echo $temp;
					}else{
						$continue = false;
					}
				}
			save_new($form_numbers);
			echo "del all ins all";
			
		}else{
		echo "yess";
			//Check if there another pupillometry form in this encounter
			//$exist = check_form($encounter, 'pupillometry');
			//if($exist==''){
				// if not, create new one
			//	$formid = addForm($encounter, "Pupillometry", /*$formid*/0, "pupillometry", $pid, $userauthorized);
			//	$query = "UPDATE forms SET form_id = id WHERE id = '$formid' AND form_id = 0";
				//echo $query;
			//	sqlStatement($query);	
			//}else{
				//get the exitant formid
			//	$formid = $exist;
			//}
			if($values['save_type']=='save_new'){
				$form_numbers[] = '1';
			}//else if($values['save_type']=='save_all'){
			//	delete_past($formid);
			//	$form_numbers = split(';', $values['save_values']);	
			//}

			save_new($form_numbers);
			echo 'save new one';
		}
	
	
	//}
	
	
	
	

?>